#include<bits/stdc++.h>
using namespace std;
const int N = 5e5+10;
int n,q;
bool bg;
#define ui unsigned int
ui s;
inline ui get(ui x) {
	x ^= x << 13;
	x ^= x >> 17;
	x ^= x << 5;
	return s = x; 
}
int f[N][22];
vector<int> bian[N];
int mdep[N],son[N];
int dep[N];
void dfs(int x,int fa){
    mdep[x] = 1;
    dep[x] = dep[fa] + 1;
    f[x][0] = fa;
    for(int i = 1;(1<<i) <= dep[x];i++){
        f[x][i] = f[f[x][i - 1]][i - 1];
    }
    for(int v : bian[x]){
        dfs(v,x);
        mdep[x] = max(mdep[v] + 1,mdep[x]);
        if(mdep[son[x]] < mdep[v]){
            son[x] = v;
        }
    }
}
int top[N];
vector<int> buf[N];
int dfn[N],dft,rnk[N];
void dfs1(int x,int tf){
    top[x] = tf;
    dfn[x] = ++dft;
    rnk[dft] = x;
    if(son[x]){
        dfs1(son[x],tf);
    }
    for(int v : bian[x]){
        if(v == son[x]){
            continue;
        }
        dfs1(v,v);
    }
    if(x == tf){
        int cur = x;
        for(int i = 1;i <= min(dep[x],mdep[x]);i++){
            buf[x].push_back(cur);
            cur = f[cur][0];
        }
    }
}
int kth(int x,int k){
    int p = 31 - __builtin_clz(k);
    int p1 = f[x][p];
    k -= dep[x] - dep[p1];
    if(k <= dep[p1] - dep[top[p1]]){
        return rnk[dfn[p1] - k];
    }else{
        k -= dep[p1] - dep[top[p1]];
        return buf[top[p1]][k];
    }
}
bool ed;
int main(){
    cerr<<(&ed - &bg)/1048576.0<<'\n';
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>q>>s;
    int rot = 0;
    for(int i = 1;i<=n;i++){
        int f;
        cin>>f;
        if(f){
            bian[f].push_back(i);
        }else{
            rot = i;
        }
    }
    dfs(rot,0);
    dfs1(rot,rot);

    int ans = 0;
    long long rans = 0;
    for(int i = 1;i<=q;i++){
        int x = (get(s) ^ ans)%n + 1,k = (get(s)^ans)%dep[x];
        if(k){
            ans = kth(x,k);
        }else{
            ans = x;
        }
        rans ^= 1ll*i*ans;
    }
    cout<<rans<<'\n';
}